package com.itheima.travel.web;

import com.github.pagehelper.PageInfo;
import com.itheima.travel.enums.StatusEnum;
import com.itheima.travel.exception.ProjectException;
import com.itheima.travel.req.FavoriteVo;
import com.itheima.travel.req.RouteVo;
import com.itheima.travel.res.ResponseWrap;
import com.itheima.travel.service.FavoriteService;
import com.itheima.travel.utils.ExceptionsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping
@Log4j2
@Api(tags = "收藏操作")
@CrossOrigin
public class FavoriteController {
    @Autowired
    FavoriteService favoriteService;

    @Value("${upLoad.webSite}")
    String webSite;

    @RequestMapping("favorite")
    @ApiOperation(value = "添加收藏",notes = "添加收藏")
    @ApiImplicitParam(name = "favoriteVo",value = "收藏信息",required = true,dataType = "FavoriteVo")
    public ResponseWrap<Integer> addFavorite(@RequestBody FavoriteVo favoriteVo) throws ProjectException {
        try {
            Integer flag = favoriteService.addFavorite(favoriteVo);
            return ResponseWrap.<Integer>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(flag)
                    .build();
        } catch (Exception e) {
            log.error("添加收藏：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(
                    StatusEnum.ADD_FAVORITE_FAIL.getCode(),
                    StatusEnum.ADD_FAVORITE_FAIL.getMsg()
            );
        }
    }

    @PostMapping("favorite/{pageNum}/{pageSize}")
    @ApiOperation(value = "我的收藏",notes = "我的收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "favoriteVo",value = "收藏查询对象",required = false,dataType = "FavoriteVo"),
            @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",dataType = "Integer"),
            @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",dataType = "Integer")
    })
    public ResponseWrap<PageInfo<RouteVo>> findMyFavorite(
            @RequestBody FavoriteVo favoriteVo,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize
    ) throws ProjectException {
        try {
            PageInfo<RouteVo> myFavorite = favoriteService.findMyFavorite(favoriteVo, pageNum, pageSize);
            return ResponseWrap.<PageInfo<RouteVo>>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .webSite(webSite)
                    .data(myFavorite)
                    .build();
        } catch (Exception e) {
            log.error("我的收藏：{}",ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(
                    StatusEnum.FIND_MYFAVORITE_FAIL.getCode(),
                    StatusEnum.FIND_MYFAVORITE_FAIL.getMsg()
            );
        }
    }

    @GetMapping("favorite/{routeId}")
    @ApiOperation(value = "是否收藏",notes = "是否收藏")
    @ApiImplicitParam(paramType = "path",name = "routeId",value = "线路Id",dataType = "Long")
    public ResponseWrap<Boolean> isFavorited(@PathVariable("routeId") Long routeId) throws ProjectException {
        FavoriteVo favoriteVo = FavoriteVo.builder().routeId(routeId).build();
        try {
            Boolean flag = favoriteService.isFavorited(favoriteVo);
            return ResponseWrap.<Boolean>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(flag)
                    .build();
        } catch (Exception e) {
            log.error("是否收藏：{}",ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(
                    StatusEnum.ISFAVORITED_FAIL.getCode(),
                    StatusEnum.ISFAVORITED_FAIL.getMsg()
            );
        }
    }
}
